<?php
namespace Home\Controller;
use Think\Controller;
class AllController extends ZMController {
	public function _initialize(){
		S(array('expire'=>21600));
	}
	public function all1(){//总产废量月度图表
		$all1=S('all1');
		if(empty($all1)&&IS_POST){	
			$Model=M();
			$region=session('user.region');
			if($region!=''){
				$like.=" and b.region=$region";
			}
			else{
				exit(0);
			}
			$res1=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,month(a.pdate) as month from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)=2017 and month(a.pdate)<10 $like group by month(a.pdate),a.kg order by a.pdate asc");
			$res2=$Model->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.qt) as qt,sum(a.guil) as guil,a.kg,month(a.pdate) as month from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)=2017 and month(a.pdate)>=10 $like group by month(a.pdate),a.kg order by a.pdate asc");
			$a=array();
			$b=array();
			$c=array();
			$d=array();
			$flag=1;
			$garbage=0;
			$lwq=0;
			$guil=0;
			foreach($res1 as $row){//1-10
				$arr=explode(',',$row['kg']);
				if($flag==$row['month']){
				    $garbage+=$arr['0']/1000*$row['garbage'];
					$garbage+=$arr['1']/1000*$row['oncething'];
					$garbage+=$arr['2']/1000*$row['nocode'];
					$garbage+=$arr['4']/1000*$row['otherthing'];
					$garbage+=$arr['8']/1000*$row['syd'];
					$garbage+=$arr['9']/1000*$row['syp'];
					$garbage+=$arr['10']/1000*$row['qt'];
					$lwq+=$arr['6']/1000*$row['lwq'];
					$lwq+=$arr['7']/1000*$row['zsq'];
					$guil+=$arr['3']/1000*$row['guil'];
				}
				else{
					array_push($a,$garbage);
				    array_push($b,$lwq);
				    array_push($d,$guil);
					$garbage=$arr['0']/1000*$row['garbage'];
					$garbage+=$arr['1']/1000*$row['oncething'];
					$garbage+=$arr['2']/1000*$row['nocode'];
					$garbage+=$arr['4']/1000*$row['otherthing'];
					$garbage+=$arr['8']/1000*$row['syd'];
					$garbage+=$arr['9']/1000*$row['syp'];
					$garbage+=$arr['10']/1000*$row['qt'];
					$lwq=$arr['6']/1000*$row['lwq'];
					$lwq+=$arr['7']/1000*$row['zsq'];
					$guil=$arr['3']/1000*$row['guil'];
					$flag++;
				}
				
			}
			array_push($a,$garbage);
			array_push($b,$lwq);
			array_push($d,$guil);
			$flag=10;
			$garbage=0;
			$lwq=0;
			$qt=0;
			$guil=0;
			foreach($res2 as $row){//10-12
				$arr=explode(',',$row['kg']);
				if($flag==$row['month']){
				    $garbage+=$arr['0']/1000*$row['garbage'];
					$lwq+=$arr['1']/1000*$row['lwq'];
					$qt+=$arr['2']/1000*$row['qt'];
					$guil+=$arr['3']/1000*$row['guil'];
				}
				else{
					array_push($a,$garbage);
				    array_push($b,$lwq);
					array_push($c,$qt);
				    array_push($d,$guil);
					$garbage=$arr['0']/1000*$row['garbage'];
					$lwq=$arr['1']/1000*$row['lwq'];
					$qt=$arr['2']/1000*$row['qt'];
					$guil=$arr['3']/1000*$row['guil'];
					$flag++;
				}	
			}
			array_push($a,$garbage);
			array_push($b,$lwq);
			array_push($c,$qt);
			array_push($d,$guil);
			$a=implode(',',$a);//感染性
			$b=implode(',',$b);//损伤性
			$c=implode(',',$c);//药物性
			$d=implode(',',$d);//病理性
			$a='['.$a.']';
			$b='['.$b.']';
			$c='[0,0,0,0,0,0,0,0,0,'.$c.']';
			$d='['.$d.']';
			$arr=array($a,$b,$c,$d);
			S('all1',$arr);
			$this->ajax($arr);
		}
		else if(!empty($all1)&&IS_POST){
			$this->ajax($all1);
		}
	}
	public function all2(){//总产废量年度图表
		$all2=S('all2');
		if(empty($all2)&&IS_POST){
			$Model=M();
			$region=session('user.region');
			if($region!=''){
				$like.=" and b.region=$region";
			}
			else{
				exit(0);
			}
			$res1=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,year(a.pdate) as year from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)>=2015 and year(a.pdate)<=2017 and a.pdate<'2017-10-01' $like group by year(a.pdate),a.kg");
			$res2=$Model->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.qt) as qt,sum(a.guil) as guil,a.kg,year(a.pdate) as year from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where a.pdate>='2017-10-01' and year(a.pdate)=2017 $like group by year(a.pdate),a.kg");
			$a=array();
			$b=array();
			$c=array();
			$d=array();
			$flag=2015;
			$garbage=0;
			$lwq=0;
			$qt=0;
			$guil=0;
			foreach($res1 as $row){//1-10
				$arr=explode(',',$row['kg']);
				if($flag==$row['year']){
				    $garbage+=$arr['0']/1000*$row['garbage'];
					$garbage+=$arr['1']/1000*$row['oncething'];
					$garbage+=$arr['2']/1000*$row['nocode'];
					$garbage+=$arr['4']/1000*$row['otherthing'];
					$garbage+=$arr['8']/1000*$row['syd'];
					$garbage+=$arr['9']/1000*$row['syp'];
					$garbage+=$arr['10']/1000*$row['qt'];
					$lwq+=$arr['6']/1000*$row['lwq'];
					$lwq+=$arr['7']/1000*$row['zsq'];
					$guil+=$arr['3']/1000*$row['guil'];
				}
				else{
					array_push($a,$garbage);
				    array_push($b,$lwq);
					array_push($c,$qt);
				    array_push($d,$guil);
					$garbage=$arr['0']/1000*$row['garbage'];
					$garbage+=$arr['1']/1000*$row['oncething'];
					$garbage+=$arr['2']/1000*$row['nocode'];
					$garbage+=$arr['4']/1000*$row['otherthing'];
					$garbage+=$arr['8']/1000*$row['syd'];
					$garbage+=$arr['9']/1000*$row['syp'];
					$garbage+=$arr['10']/1000*$row['qt'];
					$lwq=$arr['6']/1000*$row['lwq'];
					$lwq+=$arr['7']/1000*$row['zsq'];
					$guil=$arr['3']/1000*$row['guil'];
					$flag++;
				}	
			}
			array_push($a,$garbage);
			array_push($b,$lwq);
			array_push($c,$qt);
			array_push($d,$guil);
			$flag=2017;
			$garbage=0;
			$lwq=0;
			$qt=0;
			$guil=0;
			foreach($res2 as $row){//10-12
				$arr=explode(',',$row['kg']);
				if($flag==$row['year']){
				    $garbage+=$arr['0']/1000*$row['garbage'];
					$lwq+=$arr['1']/1000*$row['lwq'];
					$qt+=$arr['2']/1000*$row['qt'];
					$guil+=$arr['3']/1000*$row['guil'];
				}
				else{
					$a[count($a)-1]+=$garbage;
					$b[count($b)-1]+=$lwq;
					$c[count($c)-1]+=$qt;
					$d[count($d)-1]+=$guil;
					$garbage=$arr['0']/1000*$row['garbage'];
					$lwq=$arr['1']/1000*$row['lwq'];
					$qt=$arr['2']/1000*$row['qt'];
					$guil=$arr['3']/1000*$row['guil'];
					$flag++;
				}	
			}
			$a[count($a)-1]+=$garbage;
			$b[count($b)-1]+=$lwq;
			$c[count($c)-1]+=$qt;
			$d[count($d)-1]+=$guil;
			$a=implode(',',$a);//感染性
			$b=implode(',',$b);//损伤性
			$c=implode(',',$c);//药物性
			$d=implode(',',$d);//病理性
			$a='['.$a.']';
			$b='['.$b.']';
			$c='['.$c.']';
			$d='['.$d.']';
			$arr=array($a,$b,$c,$d);
			S('all2',$arr);
		    $this->ajax($arr);
		}
		else if(!empty($all2)&&IS_POST){
			$this->ajax($all2);
		}
	}
	/*public function all3(){//各区产废量月度图表
		$month=I('post.date');
		$all3=S($month);
		if(empty($all3)&&!empty($month)){
			$Model=M();
		    if($month<'2017-10'){//10月前
				$res1=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where date_format(a.pdate,'%Y-%m')='$month' group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			else{
				$res2=$Model->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.qt) as qt,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where date_format(a.pdate,'%Y-%m')='$month' group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			$a=array();
			$b=array();
			$c=array();
			$d=array();
			$e=array();
			$garbage=0;
			$lwq=0;
			$qt=0;
			$guil=0;
			if(!empty($res2)){//10月后
				foreach($res2 as &$row2){
					$arr=explode(',',$row2['kg']);
					$row2['garbage']=$arr['0']/1000*$row2['garbage'];
					$row2['lwq']=$arr['1']/1000*$row2['lwq'];
					$row2['guil']=$arr['2']/1000*$row2['guil'];
					$row2['qt']=$arr['3']/1000*$row2['qt'];
				}
				$arr = array();
				foreach($res2 as $v2){
					if(!isset($arr[$v2['region']])){
						$arr[$v2['region']] = $v2;
					}
					else {
						$arr[$v2['region']]['garbage'] += $v2['garbage'];
						$arr[$v2['region']]['lwq'] += $v2['lwq'];
						$arr[$v2['region']]['guil'] += $v2['guil'];
						$arr[$v2['region']]['qt'] += $v2['qt'];
					}
				}
				for($i=0;$i<16;$i++){
					$flag=0;
					foreach($arr as $r2){
						if($i==$r2['region']){
							$flag=1;
							array_push($a,$r2['garbage']);
					        array_push($b,$r2['lwq']);
					        array_push($c,$r2['qt']);
					        array_push($d,$r2['guil']);
							break;
						}
					}
					if($flag==0){
						array_push($a,$garbage);
					    array_push($b,$lwq);
					    array_push($c,$qt);
					    array_push($d,$guil);
					}
				}
			}
			else{
				foreach($res1 as &$row1){
					$arr=explode(',',$row1['kg']);
					$row1['garbage']=$arr['0']/1000*$row1['garbage'];
					$row1['garbage']+=$arr['1']/1000*$row1['oncething'];
					$row1['garbage']+=$arr['2']/1000*$row1['nocode'];
					$row1['garbage']+=$arr['4']/1000*$row1['otherthing'];
					$row1['garbage']+=$arr['8']/1000*$row1['syd'];
					$row1['garbage']+=$arr['9']/1000*$row1['syp'];
					$row1['garbage']+=$arr['10']/1000*$row1['qt'];
					$row1['lwq']=$arr['6']/1000*$row1['lwq'];
					$row1['lwq']+=$arr['7']/1000*$row1['zsq'];
					$row1['guil']=$arr['3']/1000*$row1['guil'];
				}
				$arr = array();
				foreach($res1 as $v1){
					if(!isset($arr[$v1['region']])){
						$arr[$v1['region']] = $v1;
					}
					else {
						$arr[$v1['region']]['garbage'] += $v1['garbage'];
						$arr[$v1['region']]['lwq'] += $v1['lwq'];
						$arr[$v1['region']]['guil'] += $v1['guil'];
						$arr[$v1['region']]['qt'] += $v1['qt'];
					}
				}
				for($i=0;$i<16;$i++){
					$flag=0;
					foreach($arr as $r1){
						if($i==$r1['region']){
							$flag=1;
							array_push($a,$r1['garbage']);
					        array_push($b,$r1['lwq']);
					        array_push($c,$r1['qt']);
					        array_push($d,$r1['guil']);
							break;
						}
					}
					if($flag==0){
						array_push($a,$garbage);
					    array_push($b,$lwq);
					    array_push($c,$qt);
					    array_push($d,$guil);
					}
				}
			}
			$a=implode(',',$a);//感染性
			$b=implode(',',$b);//损伤性
			$c=implode(',',$c);//药物性
			$d=implode(',',$d);//病理性
			$a='['.$a.']';
			$b='['.$b.']';
			$c='['.$c.']';
			$d='['.$d.']';
			$arr=array($a,$b,$c,$d);
			S($month,$arr);
		    $this->ajax($arr);
		}
		else if(!empty($all3)&&!empty($month)){
			$this->ajax($all3);
		}
	}
	public function all4(){//各区产废量年度图表
		$year=I('post.date');
		$all4=S($year);
		if(empty($all4)&&!empty($year)){
			$Model=M();
		    if($year<2017){//10月前
				$res1=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)=$year group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			else{
				$res1=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)=$year and a.pdate<'2017-10-01' group by b.region,a.kg having b.region is not null order by b.region asc");
				$res2=$Model->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.qt) as qt,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where year(a.pdate)=$year and a.pdate>='2017-10-01' group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			$a=array();
			$b=array();
			$c=array();
			$d=array();
			$e=array();
			$garbage=0;
			$lwq=0;
			$qt=0;
			$guil=0;
			if(!empty($res2)){//10月后
				foreach($res2 as &$row2){
					$arr=explode(',',$row2['kg']);
					$row2['garbage']=$arr['0']/1000*$row2['garbage'];
					$row2['lwq']=$arr['1']/1000*$row2['lwq'];
					$row2['guil']=$arr['2']/1000*$row2['guil'];
					$row2['qt']=$arr['3']/1000*$row2['qt'];
				}
				$arr = array();
				foreach($res2 as $v2){
					if(!isset($arr[$v2['region']])){
						$arr[$v2['region']] = $v2;
					}
					else {
						$arr[$v2['region']]['garbage'] += $v2['garbage'];
						$arr[$v2['region']]['lwq'] += $v2['lwq'];
						$arr[$v2['region']]['guil'] += $v2['guil'];
						$arr[$v2['region']]['qt'] += $v2['qt'];
					}
				}
				for($i=0;$i<16;$i++){
					$flag=0;
					foreach($arr as $r2){
						if($i==$r2['region']){
							$flag=1;
							array_push($a,$r2['garbage']);
					        array_push($b,$r2['lwq']);
					        array_push($c,$r2['qt']);
					        array_push($d,$r2['guil']);
							break;
						}
					}
					if($flag==0){
						array_push($a,$garbage);
					    array_push($b,$lwq);
					    array_push($c,$qt);
					    array_push($d,$guil);
					}
				}
			}
			else{
				foreach($res1 as &$row1){
					$arr=explode(',',$row1['kg']);
					$row1['garbage']=$arr['0']/1000*$row1['garbage'];
					$row1['garbage']+=$arr['1']/1000*$row1['oncething'];
					$row1['garbage']+=$arr['2']/1000*$row1['nocode'];
					$row1['garbage']+=$arr['4']/1000*$row1['otherthing'];
					$row1['garbage']+=$arr['8']/1000*$row1['syd'];
					$row1['garbage']+=$arr['9']/1000*$row1['syp'];
					$row1['garbage']+=$arr['10']/1000*$row1['qt'];
					$row1['lwq']=$arr['6']/1000*$row1['lwq'];
					$row1['lwq']+=$arr['7']/1000*$row1['zsq'];
					$row1['guil']=$arr['3']/1000*$row1['guil'];
				}
				$arr = array();
				foreach($res1 as $v1){
					if(!isset($arr[$v1['region']])){
						$arr[$v1['region']] = $v1;
					}
					else {
						$arr[$v1['region']]['garbage'] += $v1['garbage'];
						$arr[$v1['region']]['lwq'] += $v1['lwq'];
						$arr[$v1['region']]['guil'] += $v1['guil'];
						$arr[$v1['region']]['qt'] += $v1['qt'];
					}
				}
				for($i=0;$i<16;$i++){
					$flag=0;
					foreach($arr as $r1){
						if($i==$r1['region']){
							$flag=1;
							array_push($a,$r1['garbage']);
					        array_push($b,$r1['lwq']);
					        array_push($c,$r1['qt']);
					        array_push($d,$r1['guil']);
							break;
						}
					}
					if($flag==0){
						array_push($a,$garbage);
					    array_push($b,$lwq);
					    array_push($c,$qt);
					    array_push($d,$guil);
					}
				}
			}
			$a=implode(',',$a);//感染性
			$b=implode(',',$b);//损伤性
			$c=implode(',',$c);//药物性
			$d=implode(',',$d);//病理性
			$a='['.$a.']';
			$b='['.$b.']';
			$c='['.$c.']';
			$d='['.$d.']';
			$arr=array($a,$b,$c,$d);
			S($year,$arr);
		    $this->ajax($arr);
		}
		else if(!empty($all4)&&!empty($year)){
			$this->ajax($all4);
		}
	}
	public function wayCollect(){//处置方式统计表
		$startdate=I('startdate');
		$enddate=I('enddate');
		$Model=M();
		if(!empty($startdate)&&!empty($enddate)){
			if($startdate>='2017-10-01'){
				$startdate=$startdate;
				$on1.=" and a.pdate>='$startdate' and a.pdate<='$enddate'";
			}
			else{
				if($enddate<'2017-10-01'){
					$on2.=" and a.pdate>='$startdate' and a.pdate<='$enddate'";
				}
				else{
					$on1.=" and a.pdate>='2017-10-01' and a.pdate<='$enddate'";
					$on2.=" and a.pdate>='$startdate' and a.pdate<='2017-9-30'";
				}
			}
			if(!empty($on2)){
				$res=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where 1=1 $on2 group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			if(!empty($on1)){
				$result=$Model->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.qt) as qt,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id where 1=1 $on1 group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			if(!empty($res)){
				foreach($res as &$row1){
					$arr=explode(',',$row1['kg']);
					$row1['weight']=$arr['0']/1000*$row1['garbage'];
					$row1['weight']+=$arr['1']/1000*$row1['oncething'];
					$row1['weight']+=$arr['2']/1000*$row1['nocode'];
					$row1['weight']+=$arr['4']/1000*$row1['otherthing'];
					$row1['weight']+=$arr['8']/1000*$row1['syd'];
					$row1['weight']+=$arr['9']/1000*$row1['syp'];
					$row1['weight']+=$arr['10']/1000*$row1['qt'];
					$row1['weight']+=$arr['6']/1000*$row1['lwq'];
					$row1['weight']+=$arr['7']/1000*$row1['zsq'];
					$row1['weight']+=$arr['3']/1000*$row1['guil'];
				}
			}
			if(!empty($result)){
				foreach($result as &$row2){
					$arr=explode(',',$row2['kg']);
					$row2['weight']=$arr['0']/1000*$row2['garbage'];
					$row2['weight']+=$arr['1']/1000*$row2['lwq'];
					$row2['weight']+=$arr['2']/1000*$row2['guil'];
					$row2['weight']+=$arr['3']/1000*$row2['qt'];
					$res[]=$row2;
				}
			}
			$arr = array();
			foreach($res as $v){
				if(!isset($arr[$v['region']])){
					$arr[$v['region']] = $v;
				}
				else {					
					$arr[$v['region']]['weight'] += $v['weight'];
				}
			}
			$r=array_values($arr);
			$arr=array();
			$a['type']='集中处置';
			$b['type']='上送(小箱进大箱)';
			$c['type']='自行处置';
			foreach($r as $row){
				switch($row['region']){
					case 0://上城
					    $a['a']=$row['weight'];
					    break;
					case 1://下城
					    $a['b']=$row['weight'];
					    break;
					case 2://江干
					    if(empty($a['c'])){
					        $a['c']=$row['weight'];
						}
						else{
							$a['c']+=$row['weight'];
						}
					    break;
					case 3://拱墅
					    $a['d']=$row['weight'];
					    break;
					case 4://西湖
					    if(empty($a['e'])){
					        $a['e']=$row['weight'];
						}
						else{
							$a['e']+=$row['weight'];
						}
					    break;
					case 5://滨江
					    $a['f']=$row['weight'];
					    break;
					case 6://萧山
					    if(empty($a['g'])){
					        $a['g']=$row['weight'];
						}
						else{
							$a['g']+=$row['weight'];
						}
					    break;
					case 7://余杭
					    $a['h']=$row['weight'];
					    break;
					case 8://桐庐
					    $a['i']=$row['weight'];
					    break;
					case 9://建德
					    $a['j']=$row['weight'];
					    break;
					case 10://富阳
					    $a['k']=$row['weight'];
					    break;
					case 11://临安
					    $a['l']=$row['weight'];
					    break;
					case 12://淳安
					    $a['m']=$row['weight'];
					    break;
					case 13://算江干
					    if(empty($a['c'])){
					        $a['c']=$row['weight'];
						}
						else{
							$a['c']+=$row['weight'];
						}
					    break;
					case 14://算萧山
					    if(empty($a['g'])){
					        $a['g']=$row['weight'];
						}
						else{
							$a['g']+=$row['weight'];
						}
					    break;
					case 15://算西湖
					    if(empty($a['e'])){
					        $a['e']=$row['weight'];
						}
						else{
							$a['e']+=$row['weight'];
						}
					    break;
				}
			}
			$arr[]=$a;
			$arr[]=$b;
			$arr[]=$c;
			$this->ajax($arr);
		}
	}
	public function health(){
		$startdate=I('startdate');
		$enddate=I('enddate');
		if(!empty($startdate)&&!empty($enddate)){
			$a=$this->healthCollect($startdate,$enddate,'0');//卫生有床位
			$b=$this->healthCollect($startdate,$enddate,'1');//卫生无床位
			$arr=array();
			$arr[]=$a;
			$arr[]=$b;
			$this->ajax($arr);
		}
	}
	private function healthCollect($startdate,$enddate,$type=0){//医疗卫生机构统计
		$Model=M();
		if(!empty($startdate)&&!empty($enddate)){
			if($startdate>='2017-10-01'){
				$startdate=$startdate;
				$on1.=" and a.pdate>='$startdate' and a.pdate<='$enddate'";
			}
			else{
				if($enddate<'2017-10-01'){
					$on2.=" and a.pdate>='$startdate' and a.pdate<='$enddate'";
				}
				else{
					$on1.=" and a.pdate>='2017-10-01' and a.pdate<='$enddate'";
					$on2.=" and a.pdate>='$startdate' and a.pdate<='2017-9-30'";
				}
			}
			if($type==0){//卫生有床位
				$where.=" and b.d1=1 and b.bedcheck=1";
			}
			else{//卫生无床位
				$where.=" and b.d1=1 and b.bedcheck=0";
			}
			if(!empty($on2)){
				$res=$Model->query("select sum(a.garbage) as garbage,sum(a.oncething) as oncething,sum(a.otherthing) as otherthing,sum(a.nocode) as nocode,sum(a.syd) as syd,sum(a.qt) as qt,sum(a.syp) as syp,sum(a.lwq) as lwq,sum(a.zsq) as zsq,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id $where where 1=1 $on2 group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			if(!empty($on1)){
				$result=$Model->query("select sum(a.garbage) as garbage,sum(a.lwq) as lwq,sum(a.qt) as qt,sum(a.guil) as guil,a.kg,b.region from ddwk_collect as a left join ddwk_customer as b on a.custid=b.id $where where 1=1 $on1 group by b.region,a.kg having b.region is not null order by b.region asc");
			}
			if(!empty($res)){
				foreach($res as &$row1){
					$arr=explode(',',$row1['kg']);
					$row1['weight']=$arr['0']/1000*$row1['garbage'];
					$row1['weight']+=$arr['1']/1000*$row1['oncething'];
					$row1['weight']+=$arr['2']/1000*$row1['nocode'];
					$row1['weight']+=$arr['4']/1000*$row1['otherthing'];
					$row1['weight']+=$arr['8']/1000*$row1['syd'];
					$row1['weight']+=$arr['9']/1000*$row1['syp'];
					$row1['weight']+=$arr['10']/1000*$row1['qt'];
					$row1['weight']+=$arr['6']/1000*$row1['lwq'];
					$row1['weight']+=$arr['7']/1000*$row1['zsq'];
					$row1['weight']+=$arr['3']/1000*$row1['guil'];
					$row1['total']=$row1['garbage']+$row1['oncething']+$row1['nocode']+$row1['otherthing']+$row1['syd']+$row1['syp']+$row1['qt']+$row1['lwq']+$row1['zsq']+$row1['guil'];
				}
			}
			if(!empty($result)){
				foreach($result as &$row2){
					$arr=explode(',',$row2['kg']);
					$row2['weight']=$arr['0']/1000*$row2['garbage'];
					$row2['weight']+=$arr['1']/1000*$row2['lwq'];
					$row2['weight']+=$arr['2']/1000*$row2['guil'];
					$row2['weight']+=$arr['3']/1000*$row2['qt'];
					$row2['total']=$row2['garbage']+$row2['lwq']+$row2['guil']+$row2['qt'];
					$res[]=$row2;
				}
			}
			$arr = array();
			foreach($res as $v){
				if(!isset($arr[$v['region']])){
					$arr[$v['region']] = $v;
				}
				else {					
					$arr[$v['region']]['weight'] += $v['weight'];
					$arr[$v['region']]['total'] += $v['total'];
				}
			}
			$r=array_values($arr);
			if($type==0){
			    $a['type']='医院';
			}
			else{
				$a['type']='基层医疗机构';
			}
			foreach($r as $row){
				switch($row['region']){
					case 0://上城
					    $a['aweight']=$row['weight'];
						$a['atotal']=$row['total'];
					    break;
					case 1://下城
					    $a['bweight']=$row['weight'];
						$a['btotal']=$row['total'];
					    break;
					case 2://江干
					    if(empty($a['c'])){
					        $a['cweight']=$row['weight'];
							$a['ctotal']=$row['total'];
						}
						else{
							$a['cweight']+=$row['weight'];
							$a['ctotal']=$row['total'];
						}
					    break;
					case 3://拱墅
					    $a['dweight']=$row['weight'];
						$a['dtotal']=$row['total'];
					    break;
					case 4://西湖
					    if(empty($a['e'])){
					        $a['eweight']=$row['weight'];
							$a['etotal']=$row['total'];
						}
						else{
							$a['eweight']+=$row['weight'];
							$a['etotal']=$row['total'];
						}
					    break;
					case 5://滨江
					    $a['fweight']=$row['weight'];
						$a['ftotal']=$row['total'];
					    break;
					case 6://萧山
					    if(empty($a['g'])){
					        $a['gweight']=$row['weight'];
							$a['gtotal']=$row['total'];
						}
						else{
							$a['gweight']+=$row['weight'];
							$a['gtotal']=$row['total'];
						}
					    break;
					case 7://余杭
					    $a['hweight']=$row['weight'];
						$a['htotal']=$row['total'];
					    break;
					case 8://桐庐
					    $a['iweight']=$row['weight'];
						$a['itotal']=$row['total'];
					    break;
					case 9://建德
					    $a['jweight']=$row['weight'];
						$a['jtotal']=$row['total'];
					    break;
					case 10://富阳
					    $a['kweight']=$row['weight'];
						$a['ktotal']=$row['total'];
					    break;
					case 11://临安
					    $a['lweight']=$row['weight'];
						$a['ltotal']=$row['total'];
					    break;
					case 12://淳安
					    $a['mweight']=$row['weight'];
						$a['mtotal']=$row['total'];
					    break;
					case 13://算江干
					    if(empty($a['c'])){
					        $a['cweight']=$row['weight'];
							$a['ctotal']=$row['total'];
						}
						else{
							$a['cweight']+=$row['weight'];
							$a['ctotal']+=$row['total'];
						}
					    break;
					case 14://算萧山
					    if(empty($a['g'])){
					        $a['gweight']=$row['weight'];
							$a['gtotal']=$row['total'];
						}
						else{
							$a['gweight']+=$row['weight'];
							$a['gtotal']+=$row['total'];
						}
					    break;
					case 15://算西湖
					    if(empty($a['e'])){
					        $a['eweight']=$row['weight'];
							$a['etotal']=$row['total'];
						}
						else{
							$a['eweight']+=$row['weight'];
							$a['etotal']+=$row['total'];
						}
					    break;
				}
			}
			return $a;
		}
	}*/
}